import { LogicalSize } from '@tauri-apps/api/window';
import { WebviewWindow } from "@tauri-apps/api/WebviewWindow";

// 控制窗口
export async function controlWindow(
    windowLabel: string,
    action: 'hide' | 'show' | 'fullscreen' | 'minimize' | 'close' | 'resize',
    options?: { width?: number; height?: number}
) {
    try {
        console.log('controlWIndowD ',windowLabel);
        const window = await WebviewWindow.getByLabel(windowLabel);
        if (!window) {
            throw new Error(`窗口 ${windowLabel} 不存在！`);
        }
        switch (action) {
            case 'hide':
                await window.hide();
                break;
            case 'show':
                await window.show();
                window.setFocus();
                break;
            case 'fullscreen':
                await window.setFullscreen(!(await window.isFullscreen())); // 切换全屏状态
                break;
            case 'minimize':
                await window.minimize();
                break;
            case 'close':
                await window.close();
                break;
            case 'resize':
                if (!options?.width || !options?.height) {
                    throw new Error('调整窗口大小时必须提供 width 和 height！');
                }
                await window.setSize(new LogicalSize(options.width, options.height));
                await window.center();
                break;
            default:
                throw new Error(`未知操作: ${action}`);
        }
    } catch (error) {
        console.log(error, 'error');
        throw new Error(error instanceof Error ? error.message : '未知错误');
    }
}